<?php

$basedir = dirname(__FILE__);
require_once $basedir . '/../../../classes/magic.php';
require_once $basedir . '/../../../classes/magic/envelope.php';

class Magic_EnvelopeTest extends PHPUnit_Framework_TestCase {
    
    public function testConstructorOfMagicEnvelopeObject() {
        $object = new Magic_Envelope();
        $this->assertEquals("Magic_Envelope", get_class($object));
    }
    
    public function testCreationgOfMagicEnvelope() {
        
        $payload = '<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:activity="http://activitystrea.ms/spec/1.0/">
  <id>mimime:12345689</id>
  <title>Tuomas is now following Pamela Anderson</title>
  <content type="text/html">Tuomas is now following Pamela Anderson</content>
  <updated>2010-07-26T06:42:55+02:00</updated>
  <author>
    <uri>http://lobstermonster.org/tuomas</uri>
    <name>Tuomas Koski</name>
  </author>
  <activity:actor>
    <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
    <id>tuomas@lobstermonster.org</id>
    <title>Tuomas Koski</title>
    <link ref="alternate" type="text/html" href="http://identi.ca/tkoski"/>
  </activity:actor>
  <activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
</entry>
';
        
        $signature = "aMMmGLJd81bgBdU26WjVCT1zIH17ND0dlfArs1Kii_fVYFyz6IEyQzM3GddvzAfJ51vo-uN_RY9TEHtoHp12N9Abg9AbCcrPcBGvcP7VBhFWw857v_sYlbD6nek9cX9JKBu-C_Xf20QGuE5dPFL0S4kZsuemeQ8p6cJAj_RbumM=";        
        $magicEnvelope = Magic_Envelope::build(Magic::base64_url_encode($payload), $signature);
        $env = $magicEnvelope->asXML();
    
        $expected = '<?xml version="1.0" encoding="UTF-8"?>
<me:env xmlns:me="http://salmon-protocol.org/ns/magic-env">
  <me:data type="application/atom+xml">PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPGVudHJ5IHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDA1L0F0b20iIHhtbG5zOmFjdGl2aXR5P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toHp12N9Abg9AbCcrPcBGvcP7VBhFWw857v_sYlbD6nek9cX9JKBu-C_Xf20QGuE5dPFL0S4kZsuemeQ8p6cJAj_RbumM=</me:sig>
</me:env>
';
        $this->assertEquals($expected, $env);
    }
    
    public function testCreateFromRawXml() {
        $rawXML = '<?xml version="1.0" encoding="UTF-8"?>
<me:env xmlns:me="http://salmon-protocol.org/ns/magic-env">
  <me:data type="application/atom+xml">PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPGVudHJ5IHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDA1L0F0b20iIHhtbG5zOmFjdGl2aXR5PSJodHRwOi8vYWN0aXZpdHlzdHJlYS5tcy9zcGVjLzEuMC8iPgogIDxpZD5taW1pbWU6MTIzNDU2ODk8L2lkPgogIDx0aXRsZT5UdW9tYXMgaXMgbm93IGZvbGxvd2luZyBQYW1lbGEgQW5kZXJzb248L3RpdGxlPgogIDxjb250ZW50IHR5cGU9InRleHQvaHRtbCI-VHVvbWFzIGlzIG5vdyBmb2xsb3dpbmcgUGFtZWxhIEFuZGVyc29uPC9jb250ZW50PgogIDx1cGRhdGVkPjIwMTAtMDctMjZUMDY6NDI6NTUrMDI6MDA8L3VwZGF0ZWQ-CiAgPGF1dGhvcj4KICAgIDx1cmk-aHR0cDovL2xvYnN0ZXJtb25zdGVyLm9yZy90dW9tYXM8L3VyaT4KICAgIDxuYW1lPlR1b21hcyBLb3NraTwvbmFtZT4KICA8L2F1dGhvcj4KICA8YWN0aXZpdHk6YWN0b3I-CiAgICA8YWN0aXZpdHk6b2JqZWN0LXR5cGU-aHR0cDovL2FjdGl2aXR5c3RyZWEubXMvc2NoZW1hLzEuMC9wZXJzb248L2FjdGl2aXR5Om9iamVjdC10eXBlPgogICAgPGlkPnR1b21hc0Bsb2JzdGVybW9uc3Rlci5vcmc8L2lkPgogICAgPHRpdGxlPlR1b21hcyBLb3NraTwvdGl0bGU-CiAgICA8bGluayByZWY9ImFsdGVybmF0ZSIgdHlwZT0idGV4dC9odG1sIiBocmVmPSJodHRwOi8vaWRlbnRpLmNhL3Rrb3NraSIvPgogIDwvYWN0aXZpdHk6YWN0b3I-CiAgPGFjdGl2aXR5OnZlcmI-aHR0cDovL2FjdGl2aXR5c3RyZWEubXMvc2NoZW1hLzEuMC9mb2xsb3c8L2FjdGl2aXR5OnZlcmI-CjwvZW50cnk-Cg==</me:data>
  <me:encoding>base64url</me:encoding>
  <me:alg>RSA-SHA256</me:alg>
  <me:sig>aMMmGLJd81bgBdU26WjVCT1zIH17ND0dlfArs1Kii_fVYFyz6IEyQzM3GddvzAfJ51vo-uN_RY9TEHtoHp12N9Abg9AbCcrPcBGvcP7VBhFWw857v_sYlbD6nek9cX9JKBu-C_Xf20QGuE5dPFL0S4kZsuemeQ8p6cJAj_RbumM=</me:sig>
</me:env>
';
        $obj = Magic_Envelope::buildFromRawXML($rawXML);
        $this->assertEquals($rawXML, $obj->asXML());
        
        $expected = '<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:activity="http://activitystrea.ms/spec/1.0/">
  <id>mimime:12345689</id>
  <title>Tuomas is now following Pamela Anderson</title>
  <content type="text/html">Tuomas is now following Pamela Anderson</content>
  <updated>2010-07-26T06:42:55+02:00</updated>
  <author>
    <uri>http://lobstermonster.org/tuomas</uri>
    <name>Tuomas Koski</name>
  </author>
  <activity:actor>
    <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
    <id>tuomas@lobstermonster.org</id>
    <title>Tuomas Koski</title>
    <link ref="alternate" type="text/html" href="http://identi.ca/tkoski"/>
  </activity:actor>
  <activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
</entry>
';
        
        $this->assertEquals($expected, $obj->getDecodedData());
    }
    
    public function testSalmonFromIdentica() {
        $rawXML = '<?xml version="1.0"?>
<me:env xmlns:me="http://salmon-protocol.org/ns/magic-env"><me:data type="application/atom+xml">PD94bWwgdmVy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G9jbzpub3RlPgo8cG9jbzphZGRyZXNzPgogPHBvY286Zm9ybWF0dGVkPlBhcmlzPC9wb2NvOmZvcm1hdHRlZD4KPC9wb2NvOmFkZHJlc3M-Cjxwb2NvOnVybHM-CiA8cG9jbzp0eXBlPmhvbWVwYWdlPC9wb2NvOnR5cGU-CiA8cG9jbzp2YWx1ZT5odHRwOi8vd3d3LmxvYnN0ZXJtb25zdGVyLm9yZzwvcG9jbzp2YWx1ZT4KIDxwb2NvOnByaW1hcnk-dHJ1ZTwvcG9jbzpwcmltYXJ5Pgo8L3BvY286dXJscz4KPC9hY3Rpdml0eTphY3Rvcj4KIDxhY3Rpdml0eTp2ZXJiPmh0dHA6Ly9vc3RhdHVzLm9yZy9zY2hlbWEvMS4wL3VwZGF0ZS1wcm9maWxlPC9hY3Rpdml0eTp2ZXJiPgo8L2VudHJ5Pgo=</me:data><me:encoding>base64url</me:encoding><me:alg>RSA-SHA256</me:alg><me:sig>FdN0qsIYyc_WtNCca0KMQx2YesT4jfNULkH5wMF6uJE1dwd74_2xEh559xAvnB-siPcdDbZAUb84z7hFSbtEBfbcYmM7PZAfZQFXHM-aXomqx0mXjRnRM2YKxO6l3FCd_enErW2q8E-hDE24FACdEK6LzbJnXFoRxMCYsW8l_jA=</me:sig></me:env>';
    
        $obj = Magic_Envelope::buildFromRawXML($rawXML);
        echo $obj->getDecodedData();   
    }
    
}